package com.nf.health.app.sport_recipe.customview;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.sport_recipe.adapter.AerobicExerciseListAdapter;
import com.nf.health.app.sport_recipe.bean.EventBusChangeRecordBean;
import com.nf.health.app.sport_recipe.bean.SportRecipeDetailBean;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by he_pan on 2017/4/18.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author he_pan
 * @Description 有氧运动
 */

public class AerobicExerciseView extends BaseSportView {

    private SportRecipeDetailBean.AerobicsBean aerobicsBean;

    TextView tvFlagRecord;
    TextView tvExpend;
    RelativeLayout rlTitle;
    MyListView lvAerobicList;
    TextView tvSuggest;
    TextView tvClose;
    LinearLayout llShowData;

    private AerobicExerciseListAdapter mAdapter;

    public AerobicExerciseView(Context context, SportRecipeDetailBean bean) {
        super(context, bean);
    }

    @Override
    protected void initView() {
        View v = inflater.from(context).inflate(R.layout.aerobic_exercise, this);
        tvFlagRecord = (TextView) v.findViewById(R.id.tv_flag_record);
        tvExpend = (TextView) v.findViewById(R.id.tv_expanded);
        lvAerobicList = (MyListView) v.findViewById(R.id.lv_aerobic_list);
        tvSuggest = (TextView) v.findViewById(R.id.tv_suggest);
        tvClose = (TextView) v.findViewById(R.id.tv_close);
        llShowData = (LinearLayout) v.findViewById(R.id.ll_show_data);
        rlTitle = (RelativeLayout) v.findViewById(R.id.rl_title);
        //默认隐藏
        llShowData.setVisibility(GONE);
    }

    @Override
    protected void setUpView() {
        aerobicsBean = bean.getAerobics();
        //记录按钮的颜色与bk 0未记录 1已记录
        if ("0".equals(aerobicsBean.getFlag())) {
            tvFlagRecord.setTextColor(context.getResources().getColor(R.color.theme_color));
            tvFlagRecord.setBackgroundResource(R.drawable.hp_theme_color_border);
        } else if ("1".equals(aerobicsBean.getFlag())) {
            tvFlagRecord.setTextColor(context.getResources().getColor(R.color.gray));
            tvFlagRecord.setBackgroundResource(R.drawable.hp_gray_color_border);
        }
        //list
        mAdapter = new AerobicExerciseListAdapter(context, aerobicsBean.getList());
        lvAerobicList.setAdapter(mAdapter);
        //运动建议
        tvSuggest.setText(aerobicsBean.getSuggest());
    }

    /**
     * 设置展开收起逻辑,默认是收起的
     */
    @Override
    public void initExtendAndCloseLogic() {
        //触发展开ui逻辑或者记录逻辑接口
        rlTitle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tvFlagRecord.getVisibility() != VISIBLE) {
                    //展开
                    llShowData.setVisibility(VISIBLE);
                    tvFlagRecord.setVisibility(VISIBLE);
                    tvExpend.setVisibility(GONE);
                } else {

                }
            }
        });
        tvFlagRecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //记录
                System.out.println("记录");
                EventBus.getDefault().post(new EventBusChangeRecordBean(aerobicsBean.getType(), aerobicsBean.getFlag()));
            }
        });
        //收起
        tvClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                llShowData.setVisibility(GONE);
                tvExpend.setVisibility(VISIBLE);
                tvFlagRecord.setVisibility(GONE);
            }
        });
    }

    public void refreshFlagView(SportRecipeDetailBean bean) {
        this.bean = bean;
        setUpView();
    }
}
